package net.yeah.mickey_zhouyou.module.booklist.biz.impl;

import java.util.Collection;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

import net.yeah.mickey_zhouyou.database.DBUI;
import net.yeah.mickey_zhouyou.framework.util.JTreeUtil;
import net.yeah.mickey_zhouyou.framework.util.StringUtil;
import net.yeah.mickey_zhouyou.module.booklist.biz.CategoryManager;
import net.yeah.mickey_zhouyou.module.booklist.component.CategoryTreeNode;
import net.yeah.mickey_zhouyou.module.booklist.domain.Category;

public class CategoryManagerImpl implements CategoryManager {

	private CategoryManagerImpl() {
	}

	static private CategoryManagerImpl biz;

	public static CategoryManagerImpl getInstance() {
		if (biz == null) {
			synchronized (CategoryManagerImpl.class) {
				if (biz == null)
					biz = new CategoryManagerImpl();
			}
		}
		return biz;
	}

	private void addChildren(DefaultMutableTreeNode root,
			Collection<Category> coll) {
		if (coll.isEmpty())
			return;

		for (Category category : coll) {
			CategoryTreeNode node = new CategoryTreeNode(category);
			root.add(node);
			addChildren(node, category.children());
		}
	}

	public JTree loadTree() {
		Collection<Category> logicRootColl = DBUI.load(Category.class,
				"parentId", null);

		DefaultMutableTreeNode root = new DefaultMutableTreeNode();

		addChildren(root, logicRootColl);

		JTree tree = new JTree(new DefaultTreeModel(root));

		JTreeUtil.expandAll(tree, tree.getPathForRow(0), true);
		tree.setRootVisible(false);
		return tree;
	}

	public void addTreeRoot(JTree tree, String rootText) {
		Category category = new Category(rootText);
		CategoryTreeNode newNode = new CategoryTreeNode(category);
		DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) model.getRoot();
		model.insertNodeInto(newNode, node, node.getChildCount());

		TreeNode[] nodes = model.getPathToRoot(newNode);
		TreePath path = new TreePath(nodes);
		tree.scrollPathToVisible(path);
		DBUI.save(category);
	}

	public void addChild(JTree tree, CategoryTreeNode parentNode,
			String childNodeText) {
		if (parentNode != null && StringUtil.isNotEmpty(childNodeText)) {
			Category category = new Category(childNodeText);
			category.setParent(parentNode.getCategory());

			DefaultTreeModel model = (DefaultTreeModel) tree.getModel();

			CategoryTreeNode newNode = new CategoryTreeNode(category);
			model.insertNodeInto(newNode, parentNode,
					parentNode.getChildCount());
			TreeNode[] nodes = model.getPathToRoot(newNode);
			TreePath path = new TreePath(nodes);
			tree.scrollPathToVisible(path);
			DBUI.save(category);
		}
	}

	public void delSelectedNode(JTree tree) {
		DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
		CategoryTreeNode selectedNode = (CategoryTreeNode) tree
				.getLastSelectedPathComponent();
		model.removeNodeFromParent(selectedNode);
		DBUI.delete(Category.class, selectedNode.getCategory().getId());
	}

	public void changeNodeText(JTree tree, CategoryTreeNode node, String text) {
		if (node != null && StringUtil.isNotEmpty(text)) {
			DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
			node.getCategory().setText(text);
			node.setUserObject(text);
			model.nodeChanged(node);

			DBUI.save(node.getCategory());
		}
	}
}
